#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/types.h>
#include<fcntl.h>
#include<string.h>

void print_dir(const char *path)
{
	DIR *dir = opendir(path);
	if(!dir){	
		perror("opendir");
		return ;
	}

	struct dirent * dp = NULL;
	while(dp = readdir(dir)){
		// . and .. lue guo 
		if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
			continue;
		//dir
		if(dp->d_type==DT_DIR){
			printf("[%s]\n",dp->d_name);  //print dir
			//递归 path/dp->d_name
			char buf[256] = {};
			//strcpy(buf,path);
			//strcat(buf,"/");
			//strcat(buf,dp->d_name);	
			
			sprintf(buf,"%s/%s",path,dp->d_name);		

			print_dir(buf);
		}
		else{
			printf("file:%s\n",dp->d_name);
		}

	}
	

	closedir(dir);

}



int main(int argc,char *argv[])
{
	if(argc!=2){
		printf("%s dirpath\n",argv[0]);
		return -1;
	}
	
	print_dir(argv[1]);
	
	return 0;


}
